package com.zksk.common.security.utils;

import com.zksk.common.core.utils.SpringUtils;
import com.zksk.common.redis.service.RedisService;

import java.util.concurrent.TimeUnit;

/**
 * 生成自增得序列号码
 */
public class SeqCodeUtils {

    /**
     * 生成序列表 根据redis
     * @param key
     * @param type
     * @return
     */
    private static String createSerialNumber(String key,String type,Long initSeq) {
        RedisService redisService = SpringUtils.getBean(RedisService.class);
        Long increment = redisService.increment(key + ":" + type, initSeq);
        redisService.expire(key + ":" + type,1, TimeUnit.DAYS);
        String number = type + String.format("%05d",increment);
        return number;
    }

    private static String createOrgCode(String type, Long initSeq){
        return createSerialNumber("ORG", type, initSeq);
    }

    /**
     * 选择生成序列号得方式
     */
    public enum OrgCodeSeqType{
        YH("0","银行"){
            @Override
            public String geneSeqCode(Long initSeq) {
                return createOrgCode("YH",initSeq);
            }
        },
        BX("1","保险") {
            @Override
            public String geneSeqCode(Long initSeq) {
                return createOrgCode("BX", initSeq);
            }
        },
        DB("2","担保") {
            @Override
            public String geneSeqCode(Long initSeq) {
                return createOrgCode("DB", initSeq);
            }
        },
        QT("3","第三方") {
            @Override
            public String geneSeqCode(Long initSeq) {
                return createOrgCode("QT", initSeq);
            }
        };

        public String type;
        public String name;

        OrgCodeSeqType(String type, String name) {
            this.type = type;
            this.name = name;
        }

        public abstract String geneSeqCode(Long initSeq);
    }
}
